package com.ifnk.portaljava.hlyt.dao.impl;

import com.ifnk.portaljava.dapWeb.advance.DBAccess;
import com.ifnk.portaljava.hlyt.beans.Module;
import com.ifnk.portaljava.hlyt.beans.RolePermission;
import com.ifnk.portaljava.hlyt.dao.IRolePermissionJdbcDao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class RolePermissionJdbcDaoImpl implements IRolePermissionJdbcDao {

	public void deleteByModuleId(int moduleId, Connection conn) throws Exception {
		Statement stmt = null;
		try {
			stmt = conn.createStatement();
			String sql = "delete from role_permission where module_id="
					+ moduleId;
			System.out.println("sql:" + sql);
			stmt.execute(sql);
		} catch (SQLException e) {
			throw new Exception ("deleteByModuleId error!");
		} finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void save(RolePermission rp, int roleId, Connection conn) throws Exception {
		Statement stmt = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int id = 0;
		try {
			String idSql = "select max(id) as maxId from role_permission";
			stmt = conn.createStatement();
			rs = stmt.executeQuery(idSql);
			while (rs.next()) {
				id = rs.getInt("maxId") + 1;
			}
			String sql = "insert into role_permission(id,permission,createtime,module_id,role_id) values(?,?,now(),?,?)";
			System.out.println("insert sql:" + sql);
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, id);
			pstmt.setString(2, rp.getPermission());
			pstmt.setInt(3, rp.getModule().getId());
			pstmt.setInt(4, roleId);
			pstmt.execute();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			throw new Exception ("save RolePermission error!");
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}
				if (pstmt != null) {
					pstmt.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void deleteByRoleId(int roleId, Connection conn) throws Exception {
		Statement stmt = null;
		try {
			stmt = conn.createStatement();
			String sql = "delete from role_permission where role_id="
					+ roleId;
			System.out.println("sql:" + sql);
			stmt.execute(sql);
		} catch (SQLException e) {
			throw new Exception ("deleteByRoleId error!");
		} finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		
	}

	public List<RolePermission> findByRoleId(int roleId) throws Exception {
		DBAccess db = new DBAccess();
		ResultSet rs = null;
		List<RolePermission> rpList = new ArrayList<RolePermission>();
		try {
			String sql = "select c.*,m.id as mid,m.name as mname,m.permission as mpermission from module m,(select rp.id,rp.permission,rp.module_id from role_permission rp where rp.role_id="+roleId+" ) c where m.id=c.module_id;";
			System.out.println("sql:" + sql);
			rs = db.queryTable(sql);
			RolePermission rp = null;
			Module m = null;
			while (rs.next()) {
				rp = new RolePermission();
				rp.setId(rs.getInt("id"));
				rp.setPermission(rs.getString("permission"));
				m = new Module();
				m.setId(rs.getInt("mid"));
				m.setName(rs.getString("mname"));
				m.setPermission(rs.getString("mpermission"));
				rp.setModule(m);
				rpList.add(rp);
			}
		} catch (SQLException e) {
			throw new Exception ("findByRoleId error!");
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return rpList;
	}
	
	public static void main(String[] args) throws Exception {
		RolePermissionJdbcDaoImpl rpDao = new RolePermissionJdbcDaoImpl();
		List list = rpDao.findByRoleId(3);
		System.out.println(list.size());
	}

	public List<RolePermission> findByRoleIdList(String roleIdString) throws Exception {
		DBAccess db = new DBAccess();
		ResultSet rs = null;
		List<RolePermission> rpList = new ArrayList<RolePermission>();
		try {
			String sql = "select c.*,m.id as mid,m.name as mname,m.permission as mpermission,m.location as location from module m,(select rp.id,rp.permission,rp.module_id from role_permission rp where rp.role_id in ("+roleIdString+"0) ) c where m.id=c.module_id and m.disabletime is null;";
			System.out.println("sql:" + sql);
			rs = db.queryTable(sql);
			RolePermission rp = null;
			Module m = null;
			while (rs.next()) {
				rp = new RolePermission();
				rp.setId(rs.getInt("id"));
				rp.setPermission(rs.getString("permission"));
				m = new Module();
				m.setId(rs.getInt("mid"));
				m.setName(rs.getString("mname"));
				m.setPermission(rs.getString("mpermission"));
				m.setLocation(rs.getString("location"));
				rp.setModule(m);
				rpList.add(rp);
			}
		} catch (SQLException e) {
			throw new Exception ("findByRoleIdList error!");
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return rpList;
	}
}
